package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserChangePersonBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;

public class UserPersonQuerySame implements ServerInterface {

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		// TODO Auto-generated method stub
		try {
			MessageBean<List> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "查询成功",
					List.class);
			ChangeUserMapper mapper = factory.getMapper(ChangeUserMapper.class);
			List<String> dto = JsonUtils.jsonToList(JsonUtils.objectToJson(req.getData()), String.class);
			// 获取用户人口信息
			List<Integer> result = new ArrayList<Integer>();
			//获取重复的证件号码
			List<UserChangePersonBean> person = mapper.getUserChangePersonByCertNo(dto,dto.get(dto.size()-1));
			for (int i=0;i<dto.size()&& StringUtils.isNotBlank(dto.get(i));i++) {
				for (UserChangePersonBean bean : person) {
					if(dto.get(i)!=null && dto.get(i).equals(bean.getCertNo())) {
						result.add(i);
					}
				}
			}
			for (int i=0;i<dto.size();i++) {
				if(i!=dto.indexOf(dto.get(i)) && StringUtils.isNotBlank(dto.get(i))) {
					if(result.indexOf(dto.indexOf(dto.get(i)))==-1) {
						result.add(dto.indexOf(dto.get(i)));
					}
					if(result.indexOf(i)==-1) {
						result.add(i);
					}
				}
			}
			info.setData(result);
			return info;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			logger.error("用水人口列表查询失败", e);
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "查询失败", null);
		}
	}

}
